/*
 * @Author: xiaosihan 
 * @Date: 2023-07-22 01:53:38 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2023-07-22 03:05:20
 */

import { fabric } from "fabric";



// 打印区域元素 边长单位mm
export default class PrintArea extends fabric.Rect {


    constructor() {
        super();
    }

    width = 512;

    height = 512;

    // 边长的单位是mm
    sideLength = 500;//毫米//mm

    left = 0;

    top = 0;

    stroke = "#000000";

    strokeWidth = 1;

    strokeDashArray = [4, 4];

    clip_width = 0;

    clip_height = 0;

    clip_left = 0;

    clip_top = 0;

    render(ctx: CanvasRenderingContext2D): void {
        if (this.canvas) {
            const { sideLength } = this;
            const { width = 512, height = 512 } = this.canvas;
            ctx.save();
            ctx.fillStyle = "#00000055";
            ctx.fillRect(0, 0, width, height);
            ctx.restore();

            //擦除绘制区域
            ctx.clearRect(this.left / sideLength * 512, this.top / sideLength * 512, this.width / sideLength * 512, this.height / sideLength * 512);

            // 绘制裁剪区域
            ctx.save();
            ctx.fillStyle = "#00000055";
            const { clip_width, clip_height, clip_left, clip_top } = this;
            ctx.fillRect(clip_left / sideLength * 512, clip_top / sideLength * 512, clip_width / sideLength * 512, clip_height / sideLength * 512);
            ctx.restore();

        }

    }

}